<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancle">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleList">
      <!-- 选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancle">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListAPI, getAvatarAPI, assignRoleForemployeeAPI } from '@/api'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [],
      roleList: []
    }
  },
  created() {
    this.getRoleLis()
  },
  methods: {
    async getRoleLis() {
      const { rows } = await getRoleListAPI(1, 20)
      this.list = rows
    },
    async getRoleInfo(id) {
      try {
        const { roleIds } = await getAvatarAPI(id)
        this.roleList = roleIds
      } catch (err) {
        console.log(err)
      }
    },
    btnCancle() {
      this.$emit('update:showRoleDialog', false)
      this.roleList = [] // 清空原来的数组
    },
    async btnOk() {
      try {
        await assignRoleForemployeeAPI({ id: this.userId, roleIds: this.roleList })
        this.$emit('update:showRoleDialog', false)
        this.$message.success('更新成功')
      } catch (err) {
        console.log(err)
        this.$message.error('更新失败')
      }
    }
  }
}
</script>

<style>

</style>
